package message

import "sync"

type Context[E any, M IMessage] struct {
	entity    E
	request   M
	response  M
	interrupt bool
}

func (c *Context[E, M]) Entity() any {
	return c.entity
}

func (c *Context[E, M]) Request() IMessage {
	return c.request
}

func (c *Context[E, M]) SetRequest(request M) {
	c.request = request
}

func (c *Context[E, M]) Response() IMessage {
	return c.response
}

func (c *Context[E, M]) SetResponse(response M) {
	c.response = response
}

func (c *Context[E, M]) Interrupt() bool {
	return c.interrupt
}

func (c *Context[E, M]) SetInterrupt(interrupt bool) {
	c.interrupt = interrupt
}

var gContextPool = sync.Pool{}

func GetContext[E any, M IMessage]() *Context[E, M] {
	ctx, ok := gContextPool.Get().(*Context[E, M])
	if ok && ctx != nil {
		return ctx
	}
	return nil
}

func PutContext[E any, M IMessage](ctx *Context[E, M]) {
	if ctx == nil {
		return
	}
	gContextPool.Put(ctx)
}
